package com.example.demo.controller;

import lombok.Data;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by song on 2018/1/19.
 */
public class BaseController {
    @Data
    class State {
        public String state;
        public String url;
    }

    @Data
    class ReturnDTO {
        List data;
        long total;
    }

    //构建PageRequest
    PageRequest buildPageRequest(int pageNumber, int pagzSize) {
        return new PageRequest(pageNumber - 1, pagzSize, new Sort(Sort.Direction.DESC, "id"));
    }

    PageRequest buildPageRequest(HttpServletRequest request0) {
        String page = request0.getParameter("page");
        int pageNum = 1;
        if (page != null && page != "") {
            pageNum = Integer.parseInt(page);
        }
        String pageSizeStr = request0.getParameter("pageSize");
        int pageSize = 20;
        if (pageSizeStr != null && pageSizeStr != "") {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        return new PageRequest(pageNum - 1, pageSize, new Sort(Sort.Direction.DESC, "id"));
    }
}
